<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Address;
use App\Models\Order;
use App\Traits\ApiResponse;
use Illuminate\Http\Request;
use DB;

class OrderController extends Controller
{

    use ApiResponse;

    /**
     * [description]
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author: cuibo 2019/5/20 10:54
     */
    public function index(Request $request)
    {
        $orders = Order::query()->orderBy('order_id', 'DESC')->paginate();

        return view('admin.order.index', [
            'orders' => $orders
        ]);
    }

    public function getList(Request $request)
    {
        $orders = Order::query()->orderBy('order_id', 'DESC')->paginate();
        return $this->success($orders);
    }

    public function ship(Request $request)
    {
        $order = Order::query()->where('order_id', $request['order_id'])->first();
        if (!$order) {
            return $this->error('订单不存在');
        }
        if ($order->order_state != Order::STATUS_PAYED) {
            return $this->error('当前状态不允许发货');
        }
        $order->update(['order_state' => Order::STATUS_SHIPPING]);
        return $this->success();
    }

    public function show(Request $request)
    {
        $order = Order::query()->with('address', 'user', 'goodsDetails', 'goodsDetails.goods', 'goodsDetails.match')->where('order_id', $request['order_id'])->first();
        return view('admin.order.detail', [
            'order' => $order
        ]);
    }

}

